/*
 上部分的视图放在tableView 的头部 下部分有3个按钮
 
 主题table的数据组成
 1.cell 分为3种 买到了 没有买到 没有标注
 2.cell 的排列  首先按照用户分组，也就是section 在分组内先放没有标注的 然后放没有买到的
 在放买到的
 
 3.当修改买的状态后，修改数据 然后刷新表视图 从新加载数据
 
 4.难点是数据的排序和筛选
 
 5.默认显示全部数据 点击筛选后就切换数据
 
 有5个分组，可以放5个大的NSMutableArray,默认创建为谁买的数据，后面
 在筛选的时候创建，创建后就不再删除，重新创建了
 
 一个section数据的构成：买到了 没有买到 没有标注3组
 ｛
     ｛
 
            没有标注
      ｝
 
     ｛
            没有买到
      ｝
 
     ｛
             买到了
      ｝
 
  ｝
 一个表视图是一个NSMutableArray 里面存放着section
 一个section里面就有一个 NSMutableArray
 每个section里面都放着3个NSMutableArray
 */

#import "SKCurrentPlanViewController.h"

//没有标示的cell
#import "ProductNormalCell.h"
//买到了
#import "ProductBoughtCell.h"
//没有买到
#import "ProductNotbuyCell.h"

//按用户分类的cell的header
#import "HeaderNormalCell.h"

//用户商品购买记录
#import "UserProduct.h"

#define kNavBarHight 64
#define kBotBarHight 40

@interface SKCurrentPlanViewController () <UITableViewDataSource,UITableViewDelegate,ProductNormalCellDelegate>
{
    
    
    //默认的tableView 为为谁买
    //为谁买
    NSMutableArray *_buyuserArray;
    UITableView *_tableView;
    
    //品牌
    UITableView *_pinpaiTableView;
    NSMutableArray *_pinpaiArray;

    
    //购买地
    NSMutableArray *_buyplaceArray;
    UITableView *_buyplaceTableView;
    
    //没有买到
    NSMutableArray *_buynothingArray;
    UITableView *_buynothingTableView;
    
    
    //买到了
    NSMutableArray *_boughtArray;
    UITableView *_boughtTableView;
    
    
    BOOL isReload;
    

}

@end

@implementation SKCurrentPlanViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"当前购物计划(执行中)";
    
    //测试数据
    [self tableData];
    
    //创建表视图
    [self createTable];
    
    
    isReload = NO;
    
    
    
    _tableView.separatorColor = [UIColor yellowColor];
    
    
    
}

//本地的数据
- (void)tableData
{
    
    /*
     //用户的ID
     @synthesize  user_id = _user_id;
     //产品ID
     @synthesize  product_id = _product_id;
     //当前的购买状态 没有标识 买到的 没有买到
     @synthesize  product_state = _product_state;
     //商品的购买数量
     @synthesize  product_count = _product_count;
     //参考购买地点
     @synthesize  contrast_location = _contrast_location;
     //参考价格
     @synthesize  contrast_price = _contrast_price;
     
     
     
     
     //产品名称
     @synthesize product_name = _product_name;
     //品牌
     @synthesize pinpai = _pinpai;
     //备注
     @synthesize remarks = _remarks;
     //商品的图标
     @synthesize product_icon = _product_icon;
     //商品的图标的URL
     @synthesize imageURL = _imageURL;

     
     */
    
    //第一组
    UserProduct *product1 = [[UserProduct alloc] init];
    product1.user_id = @"阿迪";
    product1.product_name = @"皮包";
    product1.product_state = @"0";
    product1.pinpai = @"耐克";
    product1.remarks = @"黑色";
    product1.contrast_location = @"北京香山";
    product1.contrast_price = 306.0;
    product1.product_count = 1;
    
    
    UserProduct *product2 = [[UserProduct alloc] init];
    product2.user_id = @"阿迪";
    product2.product_name = @"洗衣粉";
    product2.product_state = @"0";
    product2.pinpai = @"纳爱斯";
    product2.remarks = @"超白，不伤手";
    product2.contrast_location = @"北京天坛";
    product2.contrast_price = 6.0;
    product2.product_count = 1;
    
    
    UserProduct *product3 = [[UserProduct alloc] init];
    product3.user_id = @"阿迪";
    product3.product_name = @"躲啊躲";
    product3.product_state = @"0";
    product3.pinpai = @"阿斯拉斯";
    product3.remarks = @"我噢噢噢噢";
    product3.contrast_location = @"武汉";
    product3.contrast_price = 6.0;
    product3.product_count = 1;
    
    UserProduct *product8 = [[UserProduct alloc] init];
    product8.user_id = @"阿迪";
    product8.product_name = @"发达啊躲";
    product8.product_state = @"0";
    product8.pinpai = @"阿斯萨芬的斯";
    product8.remarks = @"我噢asd飞噢噢";
    product8.contrast_location = @"地方发达";
    product8.contrast_price = 6.0;
    product8.product_count = 1;
    
    UserProduct *product9 = [[UserProduct alloc] init];
    product9.user_id = @"阿迪";
    product9.product_name = @"NBA篮球";
    product9.product_state = @"0";
    product9.pinpai = @"阿斯拉斯";
    product9.remarks = @"D大调立下了";
    product9.contrast_location = @"defalcate";
    product9.contrast_price = 6.0;
    product9.product_count = 1;
    
    
    //第二组
    UserProduct *product4 = [[UserProduct alloc] init];
    product4.user_id = @"黄晓明";
    product4.product_name = @"洗衣粉";
    product4.product_state = @"0";
    product4.pinpai = @"纳爱斯";
    product4.remarks = @"超白，不伤手";
    product4.contrast_location = @"北京天坛";
    product4.contrast_price = 6.0;
    product4.product_count = 1;
    
    UserProduct *product5 = [[UserProduct alloc] init];
    product5.user_id = @"黄晓明";
    product5.product_name = @"牙刷";
    product5.product_state = @"0";
    product5.pinpai = @"WXY";
    product5.remarks = @"呜呜呜呜";
    product5.contrast_location = @"上海";
    product5.contrast_price = 3.0;
    product5.product_count = 1;
    
    UserProduct *product6 = [[UserProduct alloc] init];
    product6.user_id = @"黄晓明";
    product6.product_name = @"棉被";
    product6.product_state = @"1";
    product6.pinpai = @"WXY";
    product6.remarks = @"呜呜呜呜";
    product6.contrast_location = @"广州";
    product6.contrast_price = 300.0;
    product6.product_count = 1;
    
    UserProduct *product7 = [[UserProduct alloc] init];
    product7.user_id = @"黄晓明";
    product7.product_name = @"床单";
    product7.product_state = @"2";
    product7.pinpai = @"WXY";
    product7.remarks = @"啊哈哈";
    product7.contrast_location = @"深圳";
    product7.contrast_price = 60.0;
    product7.product_count = 1;
    
    if (!_buyuserArray)
    {
        _buyuserArray = [NSMutableArray array];
        
        NSMutableArray *section1 = [NSMutableArray array];
      
        
        //里面有3个数组
        NSMutableArray *array1 = [NSMutableArray array];
        NSMutableArray *array2 = [NSMutableArray array];
        NSMutableArray *array3 = [NSMutableArray array];
        
        [array1 addObject:product1];
        [array1 addObject:product2];
        [array1 addObject:product3];
        [array1 addObject:product8];
        [array1 addObject:product9];
        
        
        [array2 addObject:product6];
        [array3 addObject:product7];
        
        [section1 addObject:array1];
        [section1 addObject:array2];
        [section1 addObject:array3];
        
        
        [_buyuserArray addObject:section1];
        
        NSMutableArray *section2 = [NSMutableArray array];
       
        
        //里面有3个数组
        NSMutableArray *array4 = [NSMutableArray array];
        NSMutableArray *array5 = [NSMutableArray array];
        NSMutableArray *array6 = [NSMutableArray array];
        
        [array4 addObject:product4];
        [array4 addObject:product5];
        
        [array5 addObject:product6];
        [array6 addObject:product7];
        
        [section2 addObject:array4];
        [section2 addObject:array5];
        [section2 addObject:array6];
        
        [_buyuserArray addObject:section2];
        
    }

    
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    
}

#pragma mark - create tableView
- (void)createTable
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-kNavBarHight-kBotBarHight) style:UITableViewStylePlain];
        
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        
        [self.view addSubview:_tableView];
        
        
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 190)];
        [header setBackgroundColor:[UIColor purpleColor]];
        _tableView.tableHeaderView = header;
        
    }
}




#pragma mark - table delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_tableView isEqual:tableView])
    {
        NSArray *array  = [_buyuserArray objectAtIndex:section];
        
        NSArray *array1 = [array objectAtIndex:0];
        NSArray *array2 = [array objectAtIndex:1];
        NSArray *array3 = [array objectAtIndex:2];
        
        int count = (int) [array1 count] + (int) [array2 count] + (int) [array3 count];
        
        
        if (count  > 0)
        {
               return count + 1;
        }
        else
        {
            
            return 0;
        }

    }else
    {
        return 4;

    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([_tableView isEqual:tableView])
    {
        return [_buyuserArray count];
        
    }else
    {
        return 4;
        
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    if ([_tableView isEqual:tableView])
    {
        
        NSArray *section = [_buyuserArray objectAtIndex:indexPath.section];
        
        NSArray *array1 = [section objectAtIndex:0];
        NSArray *array2 = [section objectAtIndex:1];
        NSArray *array3 = [section objectAtIndex:2];

    
        
        if (indexPath.row == 0)
        {
            
           
            
            NSArray *array;
            
            if ([array1 count] > 0)
            {
                array = array1;
                
            }else if ([array2 count] > 0)
            {
                array = array2;

            }else if([array3 count] > 0)
            {
                array = array3;

            }

            UserProduct *product = [array objectAtIndex:0];
            
            NSString *userName = [NSString stringWithFormat:@"%@",product.user_id];

            static NSString *CellIdentifier = @"HeaderNormalCellIdentifier";
            
            HeaderNormalCell *cell = (HeaderNormalCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            
            if (cell == nil)
            {
                cell = (HeaderNormalCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"HeaderNormalCell" owner:self options:nil]  lastObject];
            }
            
            cell.userName.text = userName;
            
            return cell;
            
        }
        else
        {
            NSMutableArray *array = [NSMutableArray array];
            [array addObjectsFromArray:array1];
            [array addObjectsFromArray:array2];
            [array addObjectsFromArray:array3];
            
            UserProduct *product = [array objectAtIndex:indexPath.row-1];
           
            if ([product.product_state isEqualToString:@"0"])
            {
                static NSString *CellIdentifier = @"ProductNormalCellIdentifier";
                
                ProductNormalCell *cell;
                
                if (cell == nil || isReload)
                {
                    cell = (ProductNormalCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"ProductNormalCell" owner:self options:nil]  lastObject];
                }else
                {
                    cell = (ProductNormalCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                    
                }
                
                // 向左滑动手势
                [cell addleftPanGesture];
                
                cell.productName.text = [NSString stringWithFormat:@"产品名称：%@", product.product_name];
                cell.price.text = [NSString stringWithFormat:@"%f",product.contrast_price];
                
                cell.pinPai.text = [NSString stringWithFormat:@"品牌：%@",product.pinpai];
                cell.place.text = [NSString stringWithFormat:@"参考购买地: %@",product.contrast_location];
                cell.remarks.text = [NSString stringWithFormat:@"备注: %@", product.remarks];
                
                cell.indexPath = indexPath;
                
                cell.product = product;
                
                cell.delegate = self;
                
                return cell;

            }else if([product.product_state isEqualToString:@"1"])
            {
                
                static NSString *CellIdentifier = @"ProductBoughtCellIdentifier";
                
                ProductBoughtCell *cell;
                
                if (cell == nil || isReload)
                {
                    cell = (ProductBoughtCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"ProductBoughtCell" owner:self options:nil]  lastObject];
                    
                }else
                {
                    cell = (ProductBoughtCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                    
                }
                
                // 向左滑动手势
                [cell addleftPanGesture];
                
                
                cell.productName.text = [NSString stringWithFormat:@"产品名称：%@", product.product_name];
                cell.pinPai.text = [NSString stringWithFormat:@"品牌：%@",product.pinpai];
                cell.place.text = [NSString stringWithFormat:@"参考购买地: %@",product.contrast_location];
                cell.remarks.text = [NSString stringWithFormat:@"备注: %@", product.remarks];
                
                return cell;

                
            }else
            {
                
                static NSString *CellIdentifier = @"ProductNotbuyCellIdentifier";
                
                
            
                ProductNotbuyCell *cell;
                
                if (cell == nil || isReload)
                {
                    cell = (ProductNotbuyCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"ProductNotbuyCell" owner:self options:nil]  lastObject];
                }else
                {
                    cell = (ProductNotbuyCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                }
                
                // 向左滑动手势
                [cell addleftPanGesture];
                
                
                cell.productName.text = [NSString stringWithFormat:@"产品名称：%@", product.product_name];
              
                
                cell.pinPai.text = [NSString stringWithFormat:@"品牌：%@",product.pinpai];
                cell.place.text = [NSString stringWithFormat:@"参考购买地: %@",product.contrast_location];
                cell.remarks.text = [NSString stringWithFormat:@"备注: %@", product.remarks];
                return cell;

                
            }
          
                
            
            
        }
        
    }else
    {
        if (indexPath.row == 0)
        {
            
            
            static NSString *CellIdentifier = @"HeaderNormalCellIdentifier";
            
            HeaderNormalCell *cell = (HeaderNormalCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            
            if (cell == nil)
            {
                cell = (HeaderNormalCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"HeaderNormalCell" owner:self options:nil]  lastObject];
            }
            
            
            return cell;
            
        }
        else
        {
            
            static NSString *CellIdentifier = @"ProductNormalCellIdentifier";
            
            ProductNormalCell *cell = (ProductNormalCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            
            if (cell == nil)
            {
                cell = (ProductNormalCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"ProductNormalCell" owner:self options:nil]  lastObject];
            }
            
            // 向左滑动手势
            [cell addleftPanGesture];
            
            return cell;
            
        }

    }
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0)
   {
     return 44;
       
    }else
    {
        return 98;

    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   

}


#pragma mark - ProductNormalCellDelegate
- (void)changeProduct:(UserProduct *)procuct withIndexPath:(NSIndexPath *)indexpath
{
    //找到那个section
    int section = (int) indexpath.section;
    
    procuct.product_state = @"1";
    
  
    NSMutableArray *array = [_buyuserArray objectAtIndex:section];
    //array 分为3个部分
    //移除需要标识的那项
    
    NSMutableArray *array1 = [array objectAtIndex:0];
    NSMutableArray *array2 = [array objectAtIndex:1];
    NSMutableArray *array3 = [array objectAtIndex:2];
    
    if (!array1) {
        
        array1 = [NSMutableArray array];
    }

   
    [array1 removeObjectAtIndex:indexpath.row - 1];
    [array2 addObject:procuct];
    
    
    [array removeAllObjects];
    
    [array addObject:array1];
    [array addObject:array2];
    [array addObject:array3];
    
    
    //移除那个商品项
    [_buyuserArray removeObject:[_buyuserArray objectAtIndex:section]];
    
    
    
    
    //插入
    [_buyuserArray insertObject:array atIndex:section];
    
    isReload = YES;
    [_tableView reloadData];
    

}





@end
